package cn.gxa.j229.bug.finance.service.borrowing.impl;

import cn.gxa.j229.bug.finance.mapper.LoanMapper;
import cn.gxa.j229.bug.finance.mapper.SignMapper;
import cn.gxa.j229.bug.finance.mapper.TbCheckMapper;
import cn.gxa.j229.bug.finance.pojo.Loan;
import cn.gxa.j229.bug.finance.pojo.TbCheck;
import cn.gxa.j229.bug.finance.service.borrowing.LoanService;
import cn.gxa.j229.bug.finance.utils.CreateNumber;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("loanService")
public class LoanServiceImpl implements LoanService {
    @Resource
    LoanMapper loanMapper = null;
    @Resource
    SignMapper signMapper;
    @Resource
    TbCheckMapper tbCheckMapper;

    @Override
    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.SUPPORTS,rollbackFor = {Throwable.class})
    public int deleteByNum(String num) {
        int deleteByNum = loanMapper.deleteByNum(num);
        signMapper.deleteByNum(num);
        return deleteByNum;
    }

    @Override
    public int insert(Loan record) {
        int insert = loanMapper.insert(record);
        return insert;
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.SUPPORTS,rollbackFor = {Throwable.class})
    public int insertSelective(Loan record) {
        record.setCreated(new Date());
        record.setNum(CreateNumber.getNum());
        record.setPaymoney(1.0);
        int insertSelective = loanMapper.insertSelective(record);
        //新增借款的时候为sign标插入一条同样编号的空数据，才能被后面的接口调用
        //否则会出现新增的借款无法出现在后面的页面中
        signMapper.insertNum(record.getNum());
        TbCheck tbCheck = new TbCheck();
        tbCheck.setNum(record.getNum());
        tbCheckMapper.insertSelective(tbCheck);
        return insertSelective;
    }

    @Override
    public Loan selectById(Integer id) {
        Loan selectById = loanMapper.selectById(id);
        return selectById;
    }

    @Override
    public int updateByIdSelective(Loan record) {
        int updateByIdSelective = loanMapper.updateByIdSelective(record);
        return updateByIdSelective;
    }

    @Override
    public int updateById(Loan record) {
        int updateById = loanMapper.updateById(record);
        return updateById;
    }

    @Override
    public List<Map<String, Object>> findById(Integer id) {
        List<Map<String, Object>> findById = loanMapper.findById(id);
        return findById;
    }

    @Override
    public List<Map<String, Object>> findByPage(int limit, int page) {
        Integer startRow = (limit - 1) * page;
        return loanMapper.findByPage(startRow,page);
    }

    @Override
    public Integer findByPageCount() {
        return loanMapper.findByPageCount();
    }

    @Override
    public List<Map<String, Object>> findByPhone(String phone) {
        return loanMapper.selectByPhone(phone);
    }

    @Override
    public List<Map<String, Object>> findByBorrower(String borrower) {
        return loanMapper.selectByBorrower(borrower);
    }

    @Override
    public int updating(Loan loan) {
        return loanMapper.updating(loan);
    }

    @Override
    public int fullupdate(Loan loan) {
        return loanMapper.fullupdate(loan);
    }

    @Override
    public List<Map<String, Object>> findingByPage(Loan loan) {
        Loan loan1 = new Loan();

        loan.setLimit((loan.getLimit() - 1) * loan.getPage());
//        loan.setSpeed(loan.getPaymoney()/loan.getMoney());
        return loanMapper.findingByPage(loan);
    }

    @Override
    public Integer findingByPageCount(Loan loan) {
        return loanMapper.findingByPageCount(loan);
    }

    @Override
    public List<Map<String, Object>> findFinishByPage(Loan loan) {
        loan.setLimit((loan.getLimit() - 1) * loan.getPage());
        return loanMapper.findFinishByPage(loan);
    }

    @Override
    public Integer findFinishByPageCount(Loan loan) {
        return loanMapper.findFinishByPageCount(loan);
    }

    @Override
    public List<Map<String, Object>> findAll() {
        return loanMapper.findAll();
    }

    @Override
    public List<Map<String, Object>> findLikeLoanDefend(Loan loan) {
        loan.setLimit((loan.getLimit() - 1) * loan.getPage());
        return loanMapper.findLikeLoanDefend(loan);
    }

    @Override
    public Integer findLikeLoanDefendCount(Loan loan) {
        return loanMapper.findLikeLoanDefendCount(loan);
    }

    @Override
    public List<Map<String, Object>> findLikeLoanCheck(Loan loan) {
        loan.setLimit((loan.getLimit() - 1) * loan.getPage());
        return loanMapper.findLikeLoanCheck(loan);
    }

    @Override
    public Integer findLikeLoanCheckCount(Loan loan) {
        return loanMapper.findLikeLoanCheckCount(loan);
    }

    @Override
    public int update(Loan loan) {
        return loanMapper.update(loan);
    }

    @Override
    public int updateByNumPass(String num) {
        return loanMapper.updateByNumPass(num);
    }

    @Override
    public int updateByNumUnPass(String num) {
        return loanMapper.updateByNumUnPass(num);
    }

    @Override
    public int updateByNumSelective(Loan record) {
        return loanMapper.updateByNumSelective(record);
    }
}
